﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace DotNet.Utilities.流之间的转换
{
    public static class StremHelper
    {
        #region filestream转化为memoryStream
        /// <summary>
        /// filestream转化为memoryStream
        /// </summary>
        /// <param name="fs">fileStream</param>
        /// <returns></returns>
        public static MemoryStream FileStreamChangeMemoryStream(FileStream fs)
        {
            int length = (int)fs.Length;
            byte[] data = new byte[length];
            fs.Position = 0;
            fs.Read(data, 0, length);
            MemoryStream ms = new MemoryStream(data);
            //MemoryStream ms2 = new MemoryStream(); 
            //fs.CopyTo(ms2); //转化到另外一个流里面
            return ms;
            //byte[] data = File.ReadAllBytes(filePath);
            //MemoryStream ms = new MemoryStream(data);
        }
        /// <summary>
        /// 通过路径转化为 MemoryStream
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static MemoryStream FileStreamChangeMemoryStream(string filePath)
        {
            byte[] data = File.ReadAllBytes(filePath);
            MemoryStream ms = new MemoryStream(data);
            return ms;
        }
        #endregion
        public static FileStream MemoryStreamChangeFileStream(MemoryStream ms)
        {
            //string t = "helloword";
            ////byte[] tb = Encoding.UTF8.GetBytes(t);
            //FileStream fs=new FileStream("123.txt",FileAccess.Read);
            //int length = (int)ms.Length;
            //byte[] data = new byte[length];
            //ms.Write(data, 0, data.Length);
            //Attachment at = new Attachment(ms, "test.txt");
            //FileStream fs = new FileStream(data);
            //Console.ReadKey();
            
            //return ms;
            return null;
        }
    }
}
